package com.fhz.shop.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.fhz.shop.R;

/**
 * 时间:2023/8/31
 *
 * @author Mr.Feng
 * 简述: TODO
 */
public class FentTextView extends View {

    private String s;
    private int color;

    private float dimension;
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    public FentTextView(Context context) {
        super(context);
    }

    public FentTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public void init(Context context, @Nullable AttributeSet attrs){
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FentTextView);


        s = typedArray.getString(R.styleable.FentTextView_fengText);
        color = typedArray.getColor(R.styleable.FentTextView_fengColor, getResources().getColor(R.color.black));
        dimension = typedArray.getDimension(R.styleable.FentTextView_fengSize, 15l);

        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int w = measure(widthMeasureSpec);
        int h = measure(heightMeasureSpec);

        setMeasuredDimension(w,h);

    }

    private int measure(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        switch (mode){
            case MeasureSpec.AT_MOST:
                //包裹模式
                //取到当前view的内容值的长度.
                size = 200;
                break;


            case MeasureSpec.EXACTLY://固定值和match.
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }

        int i = MeasureSpec.makeMeasureSpec(size, mode);
        return i;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
       paint.setTextSize(dimension);
       paint.setColor(color);
        canvas.drawText(s,100,100,paint);
    }
}
